/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package dBasea;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import modeloa.Peritu;

public class Peritua {

	public List<String> getPerituHiria(int idItripu) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT izena FROM Peritua where idHiria=" + idItripu
				+ "";
		ResultSet rs = dbk.executeSQL(query);
		List<String> x = new ArrayList<String>();
		while (rs.next()) {
			x.add(rs.getString(1));
		}
		rs.close();
		return x;
	}

	public List<String> getPerituIzenak() throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT izena FROM Peritua";
		ResultSet rs = dbk.executeSQL(query);
		List<String> x = new ArrayList<String>();
		while (rs.next()) {
			x.add(rs.getString(1));
		}
		rs.close();

		return x;
	}

	public boolean existitzenDaPeritua(String ideritu) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT * FROM Peritua WHERE idPeritua='" + ideritu
				+ "'";
		ResultSet rs = dbk.executeSQL(query);
		boolean aurki = rs.next();
		return aurki;
	}

	public void peritSortu(String id, String izena, String abizenak,
			Integer telefonoa, String hiri) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "INSERT INTO Peritua (idPeritua, izena, abizenak, telefonoa, idHiria)"
				+ " VALUES ('"
				+ id
				+ "','"
				+ izena
				+ "', '"
				+ abizenak
				+ "' , "
				+ telefonoa
				+ ",(Select idHiria from Hiria where izenaHiria ='"
				+ hiri
				+ "'))";

		dbk.executeSQL(query);

	}

	public Peritu getPerituDatuak(String id) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "SELECT * FROM Peritua a, Hiria h WHERE idPeritua='"
				+ id + "' and a.idHiria = h.idHiria";
		ResultSet rs = dbk.executeSQL(query);
		Peritu abokatu = null;
		if (rs.next()) {
			String nif = rs.getString("idPeritua");
			String izena = rs.getString("izena");
			String abizenak = rs.getString("abizenak");
			int telefonoa = rs.getInt("telefonoa");
			String hiria = rs.getString("izenaHiria");
			abokatu = new Peritu(nif, izena, abizenak, telefonoa, hiria);
		}
		rs.close();

		return abokatu;
	}

	public void perituAldatu(String id, String izena, String abizenak,
			Integer telefonoa, String hiri) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "UPDATE Peritua SET izena='" + izena + "', abizenak = '"
				+ abizenak + "', telefonoa =" + telefonoa
				+ ",idHiria=(Select idHiria from Hiria where izenaHiria ='"
				+ hiri + "') WHERE idPeritua = '" + id + "'";
		dbk.executeSQL(query);
	}

	public void perituEzabatu(String nif) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "DELETE FROM Peritua WHERE idPeritua='" + nif + "'";
		dbk.executeSQL(query);

	}

	public void perituaEsleitu(int istripu, String perit) throws SQLException {
		DatuBasea dbk = DatuBasea.getInstance();
		String query = "UPDATE Istripua SET idPeritua =(select idPeritua from Peritua where izena = '"
				+ perit + "') WHERE idIstripua = " + istripu + "  ";
		dbk.executeSQL(query);

	}
}
